package com.jzrh.flashbee.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jzrh.flashbee.common.core.base.BaseEntity;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author: Administrator-Jince
 * @since: 2023/11/10 13:51
 * @description:
 */
public class DefaultFieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if(Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity){
            BaseEntity baseEntity=(BaseEntity) metaObject.getOriginalObject();
            if(Objects.isNull(baseEntity.getCreateTime())){
                baseEntity.setCreateTime(LocalDateTime.now());
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object modifyTime=getFieldValByName("updateTime",metaObject);
        if(Objects.isNull(modifyTime)){
            setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        }
    }
}
